package com.bite.common.core.domain;

import com.bite.common.core.enums.ResultCode;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {
    /**
     * 这里其实就是针对ResultCode枚举类进行包裹,统一的进行结果的返回
     */

   public  String msg;

     public int code;

    //不同类型的返回值

     T data;

    public static <T> R<T> ok() {
        return assembleResult(null,ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data,ResultCode.SUCCESS);
    }

    public static <T> R<T> fail() {
        return assembleResult(null,ResultCode.FAILED);
    }
    public static <T> R<T> fail(int code,String msg) {
        return assembleResult(code,msg,null);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null,resultCode);
    }
     private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
         R r = new R();
         r.setCode(resultCode.getCode());
         r.setData(data);
         r.setMsg(resultCode.getMsg());
         return r;
     }
    private static <T> R<T> assembleResult(int code,String msg, T data) {
        R r = new R();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}
